<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>员工管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="/static/favicon.ico">
    <link href="/static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="/static/css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <link href="/static/css/animate.css" rel="stylesheet">
    <link href="/static/css/style.css?v=4.1.0" rel="stylesheet">

    <link rel="stylesheet" href="/static/plugins/bootstrap-table/bootstrap-table.min.css">
    <link rel="stylesheet" href="/static/plugins/bootstrap-validator/css/bootstrapValidator.min.css">
    <!-- Latest compiled and minified CSS -->
    <style>
        /* ibox 背景 */
        .ibox-blue > .ibox-title {
            border-color: rgb(57, 174, 245);
        }
        .form-control:focus,
        .single-line:focus {
            border-color: #0e9aef !important;
        }
        .checkbox-inline {
            line-height: 5px;
        }
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox float-e-margins ibox-blue">
        <div class="ibox-title">
            <h5>员工管理</h5>
            <div class="ibox-tools">
                <a class="collapse-link">
                    <i class="fa fa-chevron-up"></i>
                </a>
                <a class="close-link">
                    <i class="fa fa-times"></i>
                </a>
            </div>
        </div>
        <div class="ibox-content">

            <div class="row row-lg">
                <div class="example" style="margin-top: -10px;">
                    <div class="btn-group hidden-xs" id="toolbar" role="group">
                        <button type="button" class="btn btn-outline btn-default" data-toggle="modal" data-target="#addModal">
                            <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
                            添加
                        </button>
                        <button type="button" class="btn btn-outline btn-default" id="updateButton">
                            <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>
                            修改
                        </button>
                        <button type="button" class="btn btn-outline btn-default" id="deleteButton">
                            <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                            删除
                        </button>
                    </div>

                    <table id="TableEvents"></table>

                </div>
            </div>

        </div>
    </div>
</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="addModal" data-backdrop="static" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="addForm" class="form-horizontal" autocomplete="off">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">员工新增</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">员工名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name" placeholder="请输入员工名">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">工号</label>
                        <div class="col-sm-10">
                            <input type="tel" class="form-control" name="work_number" placeholder="请输入员工工号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">电话</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="tel" placeholder="请输入员工电话">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">职务</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="duty" placeholder="请输入职务">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">部门</label>
                        <div class="col-sm-10">
                            <select name="dept_id" id="addDept" class="form-control"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">用户账号</label>
                        <div class="col-sm-10">
                            <select name="sysuser_id" class="form-control">
                                <option value='' class='form-control'>--请选择--</option>
                                {volist name="user" id="vo"}
                                <option value='{$vo.id}' class='form-control'>{$vo.id}--{$vo.username}</option>
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">员工简介</label>
                        <div class="col-sm-10">
                            <textarea name="note" class="form-control"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">性别</label>
                        <div class="col-sm-10">
                            <div class="ui-radio ui-radio-primary">
                                <label>
                                    <input type="radio" name="sex" value="1">
                                    <span>男</span>
                                </label>
                                <label>
                                    <input type="radio" name="sex" value="0">
                                    <span>女</span>
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">状态</label>
                        <div class="col-sm-10">
                            <div class="ui-radio ui-radio-primary">
                                <label>
                                    <input type="radio" name="status" value="1">
                                    <span>启用</span>
                                </label>
                                <label>
                                    <input type="radio" name="status" value="0">
                                    <span>禁用</span>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-success">提交</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="updateModal" data-backdrop="static" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="updateForm" class="form-horizontal" autocomplete="off">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">修改员工</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">员工名</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="name" placeholder="请输入员工名">
                                <input type="hidden" class="form-control" name="id">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">工号</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="work_number" placeholder="请输入员工工号">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">电话</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="tel" placeholder="请输入员工电话">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">职务</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="duty" placeholder="请输入职务">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">部门</label>
                            <div class="col-sm-10">
                                <select name="dept_id" id="editDept" class="form-control"></select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户账号</label>
                            <div class="col-sm-10">
                                <select name="sysuser_id" class="form-control">
                                    <option value='' class='form-control'>--请选择--</option>
                                    {volist name="user" id="vo"}
                                    <option value='{$vo.id}' class='form-control'>{$vo.id}--{$vo.username}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">员工简介</label>
                            <div class="col-sm-10">
                                <textarea name="note" class="form-control"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">性别</label>
                            <div class="col-sm-10">
                                <div class="ui-radio ui-radio-primary">
                                    <label>
                                        <input type="radio" name="sex" value="1">
                                        <span>男</span>
                                    </label>
                                    <label>
                                        <input type="radio" name="sex" value="0">
                                        <span>女</span>
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-10">
                                <div class="ui-radio ui-radio-primary">
                                    <label>
                                        <input type="radio" name="status" value="1">
                                        <span>启用</span>
                                    </label>
                                    <label>
                                        <input type="radio" name="status" value="0">
                                        <span>禁用</span>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-success">提交</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- 全局js -->
<script src="/static/js/jquery.min.js?v=2.1.4"></script>
<script src="/static/js/bootstrap.min.js?v=3.3.6"></script>
<!-- 自定义js -->
<script src="/static/js/content.js?v=1.0.0"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="/static/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/static/plugins/bootstrap-validator/js/bootstrapValidator.min.js"></script>
<script src="/static/plugins/bootstrap-validator/js/language/zh_CN.js"></script>
<script>
    $(function () {

        getDeptList();
        $("#TableEvents").bootstrapTable({
            url: '/admin/emp/getList',
            method: 'post',
            contentType: "application/x-www-form-urlencoded",
            queryParamsType: '',//查询参数组织方式
            queryParams: queryParams,//请求服务器时所传的参数
            sidePagination: 'server',//指定服务器端分页
            search: true,
            strictSearch:true,
            searchOnEnterKey: true,
            pagination: true,
            showRefresh: true,
            showToggle: true,
            showColumns: true,
            striped: true,
            sortable: false,
            sortOrder: "asc",
            clickToSelect: true,
            pageNumber: 1,
            pageSize: 10,
            pageList: [1, 5, 10, 25, 50, 100, 'ALL'],
            iconSize: 'outline',
            toolbar: '#toolbar',
            icons: {
                refresh: 'glyphicon-repeat',
                toggle: 'glyphicon-list-alt',
                columns: 'glyphicon-list'
            },
            responseHandler: function (res) {
                //在ajax获取到数据，渲染表格之前，修改数据源
                /*console.log(res)*/
                console.log(res)
                return {"total": res.total, "rows": res.data};
            },
            columns: [
                {
                    title:'全选',
                    field:'select',
                    //复选框
                    checkbox:true,
                    width:25,
                    align:'center',
                    valign:'middle'
                },
                {
                    field: 'id',
                    title: 'ID',
                    align: 'center'
                },
                {
                    field: 'name',
                    title: '员工名',
                    align: 'center'
                },
                {
                    field: 'work_number',
                    title: '工号',
                    align: 'center'
                },
                {
                    field: 'tel',
                    title: '电话',
                    align: 'center'
                },
                {
                    field: 'duty',
                    title: '职务',
                    align: 'center'
                },
                {
                    field: 'sex',
                    title: '性别',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return '女';
                        } else if (value == 1) {
                            return '男';
                        }
                        return "其他";
                    }
                },
                {
                    field: 'note',
                    title: '员工简介',
                    align: 'center'
                },

                {
                    field: 'dname',
                    title: '所属部门',
                    align: 'center'
                },
                {
                    field: 'username',
                    title: '用户名',
                    align: 'center'
                },
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return '<span class="label label-danger">禁用</span>';
                        } else if (value == 1) {
                            return '<span class="label label-primary">启用</span>';
                        }
                        return "其他";
                    }
                },

            ]
        });

        //请求服务数据时所传参数
        function queryParams(params) {
            return {
                pageNumber: params.pageNumber,
                pageSize: params.pageSize,
                searchText: params.searchText
            }
        }

        //添加角色
        $('#addModal').on('show.bs.modal', function () {
            $("#addForm").data('bootstrapValidator').destroy();
            $('#addForm').data('bootstrapValidator', null);
            addValidator();
        });
        addValidator();
        function addValidator() {
            $('#addForm').bootstrapValidator({
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                message: '员工名不能为空'
                            },
                            stringLength: {
                                max: 30,
                                message: '员工名长度最多30个字符'
                            },
                        },
                    },
                    work_number: {
                        validators: {
                            notEmpty: {
                                message: '员工工号不能为空'
                            },
                            stringLength: {
                                max: 10,
                                message: '员工工号长度最多10个字符'
                            },
                        },
                    },
                    tel: {
                        validators: {
                            notEmpty: {
                                message: '员工电话不能为空'
                            },
                            regexp: {
                                regexp: /^1[3|5|7|8]{1}[0-9]{9}$/,
                                message: '请输入正确的手机号码'
                            }
                        },
                    },
                    duty: {
                        validators: {
                            notEmpty: {
                                message: '员工职务不能为空'
                            },
                        },
                    },
                    status: {
                        validators: {
                            notEmpty: {
                                message: '员工状态不能为空'
                            },
                        },
                    },
                    dept_id: {
                        validators: {
                            notEmpty: {
                                message: '员工部门不能为空'
                            },
                        },
                    },
                    sex: {
                        validators: {
                            notEmpty: {
                                message: '员工性别不能为空'
                            },
                        },
                    },
                    note: {
                        validators: {
                            notEmpty: {
                                message: '员工简介不能为空'
                            },
                            stringLength: {
                                max: 255,
                                message: '员工简介长度最多255个字符'
                            },
                        },
                    },
                }
            }).on('success.form.bv', function(e) {
                // Prevent form submission
                e.preventDefault();
                $.ajax({
                    url: "/admin/emp/addEmp",
                    data:  $("#addForm").serialize(),
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        $("#addModal").modal('hide');
                        if (data.success) {
                            $("#addModal").modal('hide');
                            $("#addForm")[0].reset();
                            $('#TableEvents').bootstrapTable('refresh');
                            parent.layer.msg(data.msg, {icon: 1});
                        }
                    }
                });
            });
        }

        //修改角色
        $("#updateButton").click(function () {
            //获取表格数据
            var $result = $('#TableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0 || list.length > 1) {
                parent.layer.msg('请选中一条数据', {time: 1500, icon:5});
                return;
            }
            list = list[0];
            for (var item in list) {
                if(item!='sex'&& item!='status')
                    $("#updateModal :input[name='"+item+"']").val(list[item]);
            }
            $("#updateModal input[name='sex'][value="+list.sex+"]").prop("checked",true);
            $("#updateModal input[name='status'][value="+list.status+"]").prop("checked",true);
            $("#updateForm").data('bootstrapValidator').destroy();
            $('#updateForm').data('bootstrapValidator', null);
            updateValidator();
            $("#updateModal").modal("show");
        });
        updateValidator();
        function updateValidator() {
            $('#updateForm').bootstrapValidator({
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                message: '员工名不能为空'
                            },
                            stringLength: {
                                max: 30,
                                message: '员工名长度最多30个字符'
                            },
                        },
                    },
                    work_number: {
                        validators: {
                            notEmpty: {
                                message: '员工工号不能为空'
                            },
                            stringLength: {
                                max: 10,
                                message: '员工工号长度最多10个字符'
                            },
                        },
                    },
                    tel: {
                        validators: {
                            notEmpty: {
                                message: '员工电话不能为空'
                            },
                            regexp: {
                                regexp: /^1[3|5|7|8]{1}[0-9]{9}$/,
                                message: '请输入正确的手机号码'
                            }
                        },
                    },
                    duty: {
                        validators: {
                            notEmpty: {
                                message: '员工职务不能为空'
                            },
                        },
                    },
                    status: {
                        validators: {
                            notEmpty: {
                                message: '员工状态不能为空'
                            },
                        },
                    },
                    dept_id: {
                        validators: {
                            notEmpty: {
                                message: '员工部门不能为空'
                            },
                        },
                    },
                    sex: {
                        validators: {
                            notEmpty: {
                                message: '员工性别不能为空'
                            },
                        },
                    },
                    note: {
                        validators: {
                            notEmpty: {
                                message: '员工简介不能为空'
                            },
                            stringLength: {
                                max: 255,
                                message: '员工简介长度最多255个字符'
                            },
                        },
                    },
                }
            }).on('success.form.bv', function(e) {
                // Prevent form submission
                e.preventDefault();
                $.ajax({
                    url: "/admin/emp/updateEmp",
                    data:  $("#updateForm").serialize(),
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        $("#updateModal").modal('hide');
                        if (data.success) {
                            $("#updateModal").modal('hide');
                            $('#TableEvents').bootstrapTable('refresh');
                            parent.layer.msg(data.msg, {icon: 1});
                        }
                    }
                });
            });
        }


        //删除角色
        $("#deleteButton").click(function () {
            var $result = $('#TableEvents');
            var list = $result.bootstrapTable('getSelections');
            if (list.length <= 0) {
                parent.layer.msg('请至少选中一条数据', {time: 1500, icon:5});
                return;
            }
            var id = "";
            for (var item in list) {
                id += list[item].id+",";
            }
            parent.layer.confirm('确定删除选中角色？', {
                btn: ['确定','取消'], //按钮
                shade: false //不显示遮罩
            }, function(){
                $.ajax({
                    url: "/admin/emp/deleteEmp",
                    data:  {"id": id,"status":0},
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        if (data.success) {
                            $('#TableEvents').bootstrapTable('refresh');
                            parent.layer.msg(data.msg, {icon: 1});
                        } else {
                            parent.layer.msg(data.msg, {time: 1500, icon:5});
                        }
                    }
                });
            });
        });

    });

    //查询部门列表
    function getDeptList() {
        $.ajax({
            url: "/admin/dept/getList",
            type: "POST",
            dataType: 'json',
            success: function (data) {
                addOption($("#addDept"), data);
                addOption($("#editDept"), data);
            },
            error: function (data) {

            }
        });
    }

    //创建select下拉框列表
    function addOption($obj, dataArr) {
        $obj.append("<option value='' class='form-control'>--请选择--</option>");
        //alert(JSON.stringify(dataArr));
        var str = "";
        for (var i = 0; i < dataArr.length; i++) {
            str = "<option value='" + dataArr[i].id + "' class='form-control'>" + dataArr[i].name + "</option>";
            $obj.append(str);
        }
    }

</script>
</body>
</html>